package core;

import Utils.addUtils;
import Utils.fileUtils;
import Utils.tempWorkTree;
import fileoperation.FileReader;
import gitobject.Blob;
import gitobject.Tree;
import repository.Repository;
import sha1.SHA1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Scanner;

public class JitAdd {
    //    this.workTree = path;
//        this.gitDir = path + File.separator + ".jit";
//        this.indexLoc=path+File.separator+".jit"+File.separator+"index";
//        this.commitHistory=path+File.separator+".jit"+File.separator+"logs"+File.separator+"refs"+File.separator+"heads"+File.separator+"master";


    public static void add(String filePath) throws Exception {
       
    	//如果已经有一个版本，就替换该版本，写入object里；
    	//如果没有，就查看object有无它的序列化结果，如果有就不需要写如object里。
        tempWorkTree.updateWorkTree();

        File file = new File(filePath);
        if(!file.exists()){
            System.out.println("该文件不存在");
            return;
        }
        if (file.isFile())
            addFile(file, filePath);
        else if (file.isDirectory())
            addDir(file, filePath);
    }

    public static void addFile(File file, String filePath) throws Exception { 
        boolean tag = true;
      //如果已经有一个版本，就替换该版本，写入object里；
        StringBuilder stringBuilder = new StringBuilder();
        List<String> allLines = Files.readAllLines(Paths.get(tempWorkTree.indexLoc));

        //如果文件不在缓存区内，就查看object有无它的序列化结果，如果有就不需要写如object里。
        //如果存在验证该文件有无变化，如果有变化就将新的序列化并写入object
        for (String allLine : allLines) {
            if (allLine.contains(filePath)) {
                String tempKey=SHA1.getHash(file);
                if(!FileReader.readIndexKey(allLine).equals(tempKey)){//无变化
                    if(Blob.deserialize(tempKey)==null){//写入object内
                        Blob blob = new Blob(file);
                        blob.writeObject();
                    }
                    
                    allLine = "blob" + " " + tempKey + " " + filePath+ " staged"+"\n";
                }
                tag = false;
            }
            stringBuilder.append(allLine + "\n");
            FileOutputStream fileOutputStream = new FileOutputStream(tempWorkTree.indexLoc, false);
            fileOutputStream.write(stringBuilder.toString().getBytes(StandardCharsets.UTF_8));
        }
        if (tag) {
        
            FileOutputStream fileOutputStream = new FileOutputStream(tempWorkTree.indexLoc, true);
            if (addUtils.isExistObject(file) != null) {
                //文件已存在
                Blob blob = new Blob(file);
                String tempStr = blob.getFmt() + " " + blob.getKey() + " " + filePath + " staged"+"\n";
                fileOutputStream.write(tempStr.getBytes(StandardCharsets.UTF_8));
            } else {//文件不存在，写入object
                Blob blob = new Blob(file);
                blob.writeObject();
                String tempStr = blob.getFmt() + " " + blob.getKey() + " " + filePath + " staged"+"\n";
                fileOutputStream.write(tempStr.getBytes(StandardCharsets.UTF_8));
            }
        }
    }

    public static void addDir(File dir, String filePath) throws Exception {
        File[] fs = dir.listFiles();
        List fileList = Tree.sortFile(fs);
        for (Object o : fileList) {
            File tempFile = (File) o;
            if (tempFile.isFile())
                addFile(tempFile, tempFile.getPath());
            else if (tempFile.isDirectory() && !tempFile.getName().equals(".jit")) {
                addDir(tempFile, tempFile.getPath());
            }
        }
    }
}
